var bookmanager = (() => {
    function oparator() {
        this.getBooks = (tboby) => {
            $.ajax({
                url: "/books/list",
                type: "GET",
                contentType: "application/json; charset=utf-8",
                success: (data) => {
                    this.render(tboby, data);
                }
            });
        }
        this.render = (tbody, data) => {
            tbody.children().filter("tr").remove();
            data.forEach((book) => {
                let tr = $("<tr></tr>");
                tr.attr("id", book.id);
                for (param in book) {
                    let td = $("<td></td>")
                    td.attr("name", param);
                    if (param == "price") {
                        td.text(parseFloat(book[param]).toFixed(2));
                    } else {
                        td.text(book[param]);
                    }
                    tr.append(td);
                }
                tr.dblclick((e) => {
                    e.stopPropagation();
                    let $tr = $(e.target.parentNode);
                    $("#updateBookForm").css("display", "block");
                    $("#updateBookForm").find("input[name='id']").val($tr.attr("id"));
                    $("#updateBookForm").find("input[name='bname']").val($tr.find("td[name='bname']").text());
                    $("#updateBookForm").find("input[name='price']").val($tr.find("td[name='price']").text());
                    $("#updateBookForm").find("input[name='pressDate']").val($tr.find("td[name='pressDate']").text());
                    $("#updateBookForm").find("input[name='author']").val($tr.find("td[name='author']").text());
                })
                let del = $("<button class='btn btn-default'>delete</button>")
                del.click((e) => {
                    let tr = $(e.target.parentNode);
                    let tds = tr.find("td");
                    let book = {}
                    for (i = 0; i < tds.length; i++) {
                        console.log(tds[i]);
                        book[$(tds[i]).attr("name")] = $(tds[i]).text();
                    }

                    this.deleteBook(tbody, book);
                    e.stopPropagation();
                })
                tr.append(del);
                tbody.append(tr);
            });
        }
        this.addBook = (tbody, book) => {
            $.ajax({
                url: "/books/add",
                type: "POST",
                data: JSON.stringify(book),
                contentType: "application/json; charset=utf-8",
                success: (data) => {

                    this.getBooks(tbody);
                }
            });
        }
        this.deleteBook = (tbody, book) => {
            $.ajax({
                url: "/books/delete",
                type: "POST",
                data: JSON.stringify(book),
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    console.log("#" + data.id);
                    $("#" + data.id).remove();
                }
            });
        }
        this.updateBook = (tbody, book) => {
            $.ajax({
                url: "/books/update",
                type: "POST",
                data: JSON.stringify(book),
                contentType: "application/json; charset=utf-8",
                success: (data) => {
                    $("#updateBookForm").css("display", "none");
                    this.getBooks(tbody);
                }
            });
        }
        this.createBook = (bookForm) => {
            let id = bookForm.find("input[name='id']").val();
            let bname = bookForm.find("input[name='bname']").val();
            let price = bookForm.find("input[name='price']").val();
            let pressDate = bookForm.find("input[name='pressDate']").val();
            let author = bookForm.find("input[name='author']").val();
            let book = {};
            book.id = id;
            book.bname = bname;
            book.price = parseFloat(price);
            book.pressDate = pressDate;
            book.author = author;
            return book;
        }
    }

    return new oparator();
})()

$(function () {
    bookmanager.getBooks($("tbody"));
    $("#addBookBtn").click(function (e) {
        e.preventDefault();
        let book = bookmanager.createBook($("#addBookForm"));
        console.log(book);
        bookmanager.addBook($("tbody"), book);
    })
    $("#updateBookBtn").click(function (e) {
        e.preventDefault();
        let book = bookmanager.createBook($("#updateBookForm"));
        console.log(book);
        bookmanager.updateBook($("tbody"), book);
    })
})